package com.syedu.cels.controller;

import com.syedu.cels.entity.Base;
import com.syedu.cels.entity.TestResult;
import com.syedu.cels.entity.Word;
import com.syedu.cels.service.BaseService;
import com.syedu.cels.util.InputUtil;
import com.syedu.cels.view.Ui;
import org.apache.commons.lang3.ArrayUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * description：项目客户端启动器
 * @author Administrator
 * @date 2022/3/30 13:51
 */
public class App {
    public static void main(String[] args) {
        Ui ui = new Ui();
        BaseService service = new BaseService();
        String choice;
        for (; ; ) {
            //打印主菜单
            ui.showMainMenu();
            choice = InputUtil.getStr("请您选择>");
            if ("1".equals(choice)) {
                for (; ; ) {
                    //查看词库
                    ui.showBaseMenu();
                    choice = InputUtil.getStr("请您选择>");
                    if ("1".equals(choice)) {
                        //浏览单词
                        Map<Character, List<Word>> wordsMap = service.getWordsMap();
                        Set<Character> characters = wordsMap.keySet();
                        int newLine = 0;
                        for (Character c : characters) {
                            System.out.print(Character.toUpperCase(c) + "[" + wordsMap.get(c).size() + "]" + "\t");
                            newLine++;
                            if (newLine % 10 == 0) {
                                System.out.println();
                            }
                        }
                        System.out.println();
                        String firstLetter = InputUtil.getStr("请您输入想要查看单词的首字母>");
                        //总页数
                        int totalPage = (int) Math.ceil(wordsMap.get(firstLetter.charAt(0)).size() / 10);
                        int pageNow = 1;
                        for (; ; ) {
                            service.findWordsByPage(firstLetter, pageNow, 10).forEach(System.out::println);
                            if (pageNow > totalPage) {
                                System.out.println("已到达列表末尾...");
                                break;
                            }
                            choice = InputUtil.getStr("是否显示下一页(Y/N)>");
                            if ("y".equalsIgnoreCase(choice)) {
                                pageNow++;
                            } else {
                                break;
                            }
                        }
                    } else if ("2".equals(choice)) {
                        //浏览词汇...
                        int vocabularyCount = service.vocabularyCount();
                        int totalPage = (int) Math.ceil(vocabularyCount / 10.0);
                        System.out.println("词汇列表,共计[" + vocabularyCount + "]个词汇,总页数[" + totalPage + "]...");
                        int pageNow = 1;
                        for (; ; ) {
                            service.findVocabulariesByPage(pageNow, 10).forEach(System.out::println);
                            if (pageNow > totalPage) {
                                System.out.println("已到达列表末尾...");
                                break;
                            }
                            choice = InputUtil.getStr("是否显示下一页(Y/N)>");
                            if ("y".equalsIgnoreCase(choice)) {
                                pageNow++;
                            } else {
                                break;
                            }
                        }
                    } else if ("0".equals(choice)) {
                        break;
                    } else {
                        System.out.println("对不起,您的选择有误,请重新输入...");
                    }
                }
            } else if ("2".equals(choice)) {
                //水平测试
                for (; ; ) {
                    ui.showTestMenu();
                    choice = InputUtil.getStr("请您选择>");
                    if ("1".equals(choice)) {
                        //英文->中文
                        //准备试卷,产生10道题目的试卷
                        List<Base> tests = service.createTest(10);
                        //做题
                        int rightNum = 0;
                        double rank = 0.0;
                        String desc = null;
                        for (int i = 0; i < tests.size(); i++) {
                            String question = tests.get(i).getEn();
                            String[] answer = tests.get(i).getCns();
                            String myAnswer = InputUtil.getStr((i + 1) + "." + question + "的中文是: ");
                            //判断答案是否正确
                            boolean r = ArrayUtils.contains(answer, myAnswer);
                            if (r) {
                                System.out.println("正确!!!");
                                rightNum++;
                            } else {
                                System.out.println("错误, " + question + "的中文是:" + Arrays.toString(answer));
                                //TODO
                                //存生词表...
                                service.shen(question+Arrays.toString(answer));

                            }
                        }
                        //输出测试结果
                        if (rightNum >= 8) {
                            desc = "非常棒!";
                        } else if (rightNum >= 5) {
                            desc = "还可以!";
                        } else if (rightNum >= 3) {
                            desc = "不理想!";
                        } else {
                            desc = "菜!!!";
                        }
                        System.out.println("一共10题,正确" + rightNum + "题,错误" + (10 - rightNum) + "题,正确率" + (rightNum / 10.0) + ",评价: " + desc);
                        //把成绩结果保存到记录文件中
                        //1.封装成绩对象
                        TestResult testResult = new TestResult();
                        testResult.setTotalQuestion(10);
                        testResult.setRightAnswer(rightNum);
                        testResult.setWrongAnswer(10 - rightNum);
                        testResult.setDescription(desc);
                        testResult.setTestTime(LocalDateTime.now());
                        //2.写入文件
                        service.logTest(testResult);
                    } else if ("2".equals(choice)) {
                        //中-->英
                        List<Base> tests = service.createTest(10);
                        //做题
                        int rightNum = 0;
                        double rank = 0.0;
                        String desc = null;
                        for (int i = 0; i < tests.size(); i++) {
                            String question = tests.get(i).getEn();
                            String[] answer = tests.get(i).getCns();
                            String myAnswer = InputUtil.getStr((i + 1) + "." + Arrays.toString(answer) + "的英文是: ");
                            //判断答案是否正确

                            if (myAnswer==question) {
                                System.out.println("正确!!!");
                                rightNum++;
                            } else {
                                System.out.println("错误, " + Arrays.toString(answer) + "的中文是:" + question);
                                //TODO
                                //存生词表...
                                service.shen(question+Arrays.toString(answer));
                            }
                        }
                        //输出测试结果
                        if (rightNum >= 8) {
                            desc = "非常棒!";
                        } else if (rightNum >= 5) {
                            desc = "还可以!";
                        } else if (rightNum >= 3) {
                            desc = "不理想!";
                        } else {
                            desc = "菜!!!";
                        }
                        System.out.println("一共10题,正确" + rightNum + "题,错误" + (10 - rightNum) + "题,正确率" + (rightNum / 10.0) + ",评价: " + desc);
                        //把成绩结果保存到记录文件中
                        //1.封装成绩对象
                        TestResult testResult = new TestResult();
                        testResult.setTotalQuestion(10);
                        testResult.setRightAnswer(rightNum);
                        testResult.setWrongAnswer(10 - rightNum);
                        testResult.setDescription(desc);
                        testResult.setTestTime(LocalDateTime.now());
                        //2.写入文件
                        service.logTest(testResult);
                    } else if ("3".equals(choice)) {
                        //查看历史记录...
                        service.showTestResult();
                    } else if ("0".equals(choice)) {
                        //返回上一层
                        break;
                    } else {
                        System.out.println("对不起,您的选择有误,请重新输入...");
                    }
                }
            } else if ("3".equals(choice)) {
                for(;;){
                ui.showshen();
                    choice = InputUtil.getStr("请您选择>");
               if ("1".equals(choice)){
                   service.showshen();
               }
                else if ("2".equals(choice)){


               }
                }
            } else if ("4".equals(choice)) {
                System.out.println("谢谢使用!");
                break;
            } else {
                System.out.println("您的输入有误,请重新输入...");
            }
        }
    }
}